package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/21 15:04
 * @description：https://leetcode-cn.com/problems/sliding-window-maximum/
 */
public class 滑动窗口最大值 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.maxSlidingWindow(new int[]{1}, 1)));
    }

    class Solution {
        public int[] maxSlidingWindow(int[] nums, int k) {
            int len = nums.length;
            int[] ans = new int[len + 1 - k];
            PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o2 - o1);
            for (int i = 0; i < len; i++) {
                if (i >= k) {
                    ans[i - k] = queue.peek();
                    queue.remove(nums[i - k]);
                }
                queue.offer(nums[i]);
            }
            ans[ans.length - 1] = queue.peek();
            return ans;
        }
    }
}
